<?php

namespace App\Http\Controllers\Web;

use App\Models\Admin\Members;
use App\Models\Admin\MemberInfo;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Carbon\Carbon;
use Auth;
use DB;

class MemberController extends Controller
{
    public $result = array("status"=>0,'msg'=>'请求成功','data'=>"");

    //用户列表
    public function index(Request $request)
    {
        if ($request->post()) {
            $data["name"]       = $request->input("username");
	        $data["mobile"]     = $request->input("mobile");
	        $data["pic"]        = $request->input("pic");
	        $data["email"]      = $request->input("email");
	        $data["password"]   = $request->input("password");
	        $data["password_confirmation"]   = $request->input("password_confirmation");
	        
	        //校验密码
	        if($data["password"]!=""){
	            if ($data["password"] != '' && $data["password_confirmation"] != '' && $data["password"] == $data["password_confirmation"]) {
	                $data["password"] = bcrypt($request->input('password'));
	                unset($data["password_confirmation"]);
	            } else {
	                return redirect()->back()->withErrors('密码或确认密码不能为空,且两个密码要一致！');
	            }
	        }else{
	            unset($data["password_confirmation"]);
	            unset($data["password"]);
	        }
	        
	        $data["qq"]         = $request->input("qq");
	        $data["wechat"]     = $request->input("wechat");
	        $data["company"]   = $request->input("company");
	        $data["position"]   = $request->input("position");
	        
	        $bool1 = Members::where('id','=',Auth::guard('web')->user()->id)->update($data);
	        if($bool1){
	        	return redirect('/member/index')->withSuccess('修改成功！');
	        }else{
	        	return redirect('/member/index')->withErrors('修改失败！');
	        }
        }
        
        $data = Members::from('members as m')
        ->select('m.name as username','m.*')
    	->where('m.id','=',Auth::guard('web')->user()->id)
    	->first();
    	
    	return view('web.member.index', $data);
    }
    
}
